package task06;

import java.util.ArrayList;
import java.util.List;

public class FactorizerComputable implements Computable<Integer, List> {
    private List<Integer> simpleNumbers;
    {
        generateSimpleNumbers(100);
    }

    private void generateSimpleNumbers(int maxNumber) {
        simpleNumbers = new ArrayList<>();
    outer:
        for (int number = 1; number < maxNumber; number++) {
            for (int factor = 2; factor < number - 1; factor++) {
                if (number % factor == 0)
                    continue outer;
            }
            simpleNumbers.add(number);
        }
    }

    private List<Integer> factorize(int number) {
        List<Integer> simpleFactors = new ArrayList<>();
        while (number > 1) {
            for (int simpleNumber : simpleNumbers) {
                if (simpleNumber == 1)
                    continue;
                if (number % simpleNumber == 0) {
                    number = number / simpleNumber;
                    simpleFactors.add(simpleNumber);
                    break;
                }

            }
        }
        return simpleFactors;
    }

    @Override
    public List compute(Integer number) throws InterruptedException {
        return this.factorize(number);
    }

    public static void main(String[] args) throws InterruptedException {
        FactorizerComputable factorizerComputable = new FactorizerComputable();
        for (int i = -1; i < 100; i++) {
            System.out.println(i);
            System.out.println(factorizerComputable.compute(i));
            System.out.println();
        }
    }
}
